package com.pangolin.pangolinim.net;

import com.pangolin.pangolinim.utils.ConvertUtil;

import java.util.Arrays;

/**
 * @desc    二进制协议解码类，解析C++结构体
 * @version 1.0
 */

public class BinaryReadStream {
    private byte[] _data;
    //当前数据指针
    private int	   _cur;

    BinaryReadStream(byte[] data) {
        _data = data;
        _cur = 0;
    }

    public Boolean isEmpty() {
        return _data.length <= 0 ? true : false;
    }

    public int getSize() {
        return _data.length;
    }

    public int readInt() {
        if (_cur + 4 > _data.length)
            return 0;

        int tmp = ConvertUtil.bytesToInt(_data , _cur);
        _cur += 4;
        return tmp;
    }

    public short readShort() {
        if (_cur + 2 > _data.length)
            return 0;

        short tmp =  ConvertUtil.bytesToShort(_data , _cur);
        _cur += 2;
        return tmp;
    }

    public String readString(){
        //包头长度为16
        if (_cur + 16 > _data.length)
            return null;
        byte[] new_data= Arrays.copyOfRange(_data, 16, _data.length);
        String data = new String(new_data);
        return data;
    }
}
